﻿using Avalonia.Controls;
using Avalonia.VisualTree;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LightCAD.UI
{
    public class SideBarItem
    {
        public string Guid { get; set; }
        public string Title { get; set; }
        public string Tooltip { get; set; }
        public Action Click { get; set; }
        public Control Content { get; set; }
        public bool ContentCanResize { get; set; }
        public bool ContentCanPin { get; set; }
        public bool IsEnabled { get; set; }

        public object Tag { get; set; }

    }

    public class SideBarViewModel
    {
        public SideBarViewModel()
        {
            Items = new ObservableCollection<SideBarItem>();
        }
        public ObservableCollection<SideBarItem> Items { get; }
        public BarSide Side { get; internal set; }
    }
    public enum BarSide
    {
        Left,
        Right
    }
    public class SideBarManager
    {
        private Dictionary<SideBarItem, SideContent> sideItems = new Dictionary<SideBarItem, SideContent>();
        internal SideBarManager(MainWindow window)
        {
            _window = window;
            _leftPanel = window.leftPanel;
            _rightPanel = window.rightPanel;
        }
        private DockPanel _leftPanel;
        private DockPanel _rightPanel;
        private MainWindow _window;
        public MainWindow Window => _window;

        internal void Initilize()
        {
            _leftPanel.DataContext = new SideBarViewModel { Side = BarSide.Left };
            _rightPanel.DataContext = new SideBarViewModel { Side = BarSide.Right };
        }
        public SideBarViewModel LeftModel
        {
            get
            {
                return _leftPanel.DataContext as SideBarViewModel; ;
            }
        }
        public SideBarViewModel RightModel
        {
            get
            {
                return _rightPanel.DataContext as SideBarViewModel; ;
            }
        }

        public void AddItem(SideBarItem barItem, BarSide side= BarSide.Left)
        {
            var model =(side== BarSide.Left)?LeftModel: RightModel;
            model.Items.Add(barItem);
        }

        internal void ItemClick(Button? button)
        {
            var panel = button.FindAncestorOfType<DockPanel>(false).Parent as DockPanel;
            var side = (panel.Name == "leftPanel") ? BarSide.Left : BarSide.Right;
            var sbarItem = button.DataContext as SideBarItem;
            sbarItem.Click?.Invoke();
            if (sbarItem.Content == null) return;

            if ( !sideItems.ContainsKey(sbarItem))
            {
                SideContent sc = new SideContent();
                sc.Content = sbarItem.Content;
                if (side == BarSide.Left)
                {
                    sc.SetValue(DockPanel.DockProperty, Dock.Left);
                    _window.leftArea.Children.Add(sc);
                }
                else
                {
                    sc.SetValue(DockPanel.DockProperty, Dock.Right);
                    _window.rightArea.Children.Add(sc);
                }
                sideItems.Add(sbarItem, sc);
            }


        }
    }
}
